package com.jzsec.proxy.jzsecproxy.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct;

@Configuration
public class RedisConfiguration {

	private Integer minIdle = 0;
	private Integer maxIdle = 8;
	private Integer maxTotal = 8;
	private Long redisTimeout = -1L;
	private Integer redisPort;
	private String redisHost;
	private String redisPassword;

	@Autowired
	private Environment env;
	@PostConstruct
	private void init() {
		minIdle = Integer.valueOf(env.getProperty("spring.redis.jedis.pool.min-idle", minIdle.toString()));
		maxIdle = Integer.valueOf(env.getProperty("spring.redis.jedis.pool.max-idle", maxIdle.toString()));
		maxTotal = Integer.valueOf(env.getProperty("spring.redis.jedis.pool.max-total", maxTotal.toString()));
		redisTimeout = Long.valueOf(env.getProperty("spring.redis.timeout", redisTimeout.toString()));

		redisPort = Integer.valueOf(env.getProperty("spring.redis.port"));
		redisHost = env.getProperty("spring.redis.host");
		redisPassword = env.getProperty("spring.redis.password", "");
	}

	private RedisConnectionFactory connectionFactory = null;

	@Bean(name = "redisConnectionFactory")
	public RedisConnectionFactory initRedisConnectionFactory() {
		if (this.connectionFactory != null) {
			return this.connectionFactory;
		}
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		poolConfig.setMinIdle(minIdle);
		poolConfig.setMaxIdle(maxIdle);  // 最大空闲数
		poolConfig.setMaxTotal(maxTotal); // 最大连接数
		poolConfig.setMaxWaitMillis(redisTimeout);  // 最大等待毫秒数
		JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb =
				(JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
		jpcb.poolConfig(poolConfig);
		// 通过构造器来构造jedis客户端配置
		JedisClientConfiguration jedisClientConfiguration = jpcb.build();

		// 单机版jedis
		RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
		redisStandaloneConfiguration.setHostName(redisHost);
		redisStandaloneConfiguration.setPort(redisPort);
		redisStandaloneConfiguration.setDatabase(0);
		redisStandaloneConfiguration.setPassword(redisPassword);

		// 单机配置 + 客户端配置 = jedis连接工厂
		RedisConnectionFactory connectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);

		this.connectionFactory = connectionFactory;
		return connectionFactory;
	}

	@Bean(name = "redisTemplate")
	public RedisTemplate<Object, Object> initRedisTemplate() {
		RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
		RedisSerializer stringRedisSerializer = redisTemplate.getStringSerializer();
		// 或设置所有的默认序列化器为 string
		redisTemplate.setDefaultSerializer(stringRedisSerializer);
		redisTemplate.setConnectionFactory(initRedisConnectionFactory());

		return redisTemplate;
	}
}
